//
//  MyViewControllerV2.m
//  FlashCards
//
//  Created by Home on 2/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyViewControllerV2.h"
#import "MyCardViewController.h"
#import "FlashCards.h"
#import "DebugMacro.h"

@implementation MyViewControllerV2
@synthesize Scroller;
@synthesize viewControllers;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [Scroller setScrollEnabled:YES];
    
    Scroller.showsHorizontalScrollIndicator = NO;
    Scroller.showsVerticalScrollIndicator = NO;
    Scroller.scrollsToTop = NO;
    Scroller.pagingEnabled = YES;
    Scroller.delegate = self;

    int arraySize = [[FlashCards sharedFlashCards].contentList count];

    [Scroller setContentSize:CGSizeMake((320*arraySize),460)];
    
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 


    
    
    // view controllers are created lazily
    // in the meantime, load the array with placeholders which will be replaced on demand
    
    viewControllers = [[NSMutableArray alloc] init];
    
 
    
    for (int i = 0; i < arraySize; i++ ) {
        
        
        [FlashCards sharedFlashCards].currentCardNumber = i + 1;
        
        MyCardViewController *mcvc = [storyboard instantiateViewControllerWithIdentifier:@"MyCardViewController"];
        [viewControllers addObject:mcvc];

        
        [mcvc.view setFrame:CGRectMake((320*i),0,320,460)];
        
        [Scroller addSubview:mcvc.view];
        
        // [controller.Mcvc playSound:nil];
        
    }

    // Set at the first card
    [FlashCards sharedFlashCards].currentCardNumber = 1;
    [ [viewControllers objectAtIndex:0] playSound:nil];

     


    

    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [self setScroller:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



- (void)scrollViewDidScroll:(UIScrollView *)sender
{

    

    CGFloat pageWidth = Scroller.frame.size.width;
    int page = floor((Scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    
    // they try and scroll left and they are on the first card, return back to the 
    // main window.
    if ( page < 0 ) {
        
        [self dismissModalViewControllerAnimated:YES];
        return;
    }
    
    // Don't scroll past the last card
    if ( page >= [[FlashCards sharedFlashCards].contentList count] ) {
        return;
    }

    // When Scrolling, keep track of the current page number for other uses....

    if ( [FlashCards sharedFlashCards].currentCardNumber != (page + 1 )) {
        DebugLog(@"Card number changed to %d",page+1);
        [FlashCards sharedFlashCards].currentCardNumber = page + 1;
        [ [viewControllers objectAtIndex:page] playSound:nil];

    }
    

     
    
    // A possible optimization would be to unload the views+controllers which are no longer visible
}

- (void)loadScrollViewWithPage:(int)page
{
    
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

	
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   	
}

- (IBAction)changePage:(id)sender
{
   
}




@end
